package com.itheima.tilas.controller;

import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.annotation.Log;
import com.itheima.tilas.mapper.StudentMapper;
import com.itheima.tilas.po.*;
import com.itheima.tilas.service.StudentService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 学生表服务层
 *
 * @author 132
 */
@RestController
@RequestMapping("/students")
@CrossOrigin
public class StudentController {
    @Resource
    StudentService studentService;

    @GetMapping
    public ResultUtil selectStudentPage(SelectStudentPageParam param) {
        return studentService.selectStudentPage(param);

    }

    @PostMapping
    @Log
    public ResultUtil insertStudentOne(@RequestBody InsertStudentParam param) {
        int flag = studentService.insertStudentOne(param);
        return flag > 0 ? ResultUtil.success("插入成功") : ResultUtil.fail("插入失败");
    }

    @DeleteMapping("/{ids}")
    @Log
    public ResultUtil batchDeleteStudentIds(@PathVariable List<Integer> ids) {
        if (Objects.isNull(ids) || ids.isEmpty()) {
            return ResultUtil.fail("删除失败");
        }
        int flag = studentService.batchDeleteStudentByIds(ids);
        if (CollectionUtils.isNotEmpty(ids)) {
            return ResultUtil.success("删除" + flag + "条信息成功");
        } else {
            return ResultUtil.fail("删除失败请检查输入的参数");
        }
    }

    @GetMapping("/{id}")
    public ResultUtil selectStudentById(@PathVariable Integer id) {
        if (Objects.isNull(id)) {
            return ResultUtil.fail("查询失败请检查参数");
        }
        return ResultUtil.success(studentService.selectStudentById(id));
    }

    @PutMapping
    @Log
    public ResultUtil updateStudentById(@RequestBody UpdateStudentByIdParam param) {
        if (Objects.isNull(param)) {
            return ResultUtil.fail("更新失败请检查参数");
        }
        int flag = studentService.updateStudentById(param);
        return flag > 0 ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @PutMapping("/violation/{id}/{score}")
    @Log
    public ResultUtil updateStudentPunishById(@PathVariable("id") Integer id,@PathVariable("score") Double score){
        UpdateStudentPunishByIdParam param = new UpdateStudentPunishByIdParam();
        param.setScore(score);
        param.setId(id);
        int flag = studentService.updateStudentPunishById(param);
        return flag >0 ? ResultUtil.success("处理学生违纪成功"):ResultUtil.fail("处理学生违纪失败");
    }
}
